package com.enjoyiot.module.system.controller.admin.sp;

import com.enjoyiot.framework.common.pojo.CommonResult;
import com.enjoyiot.framework.common.pojo.PageResult;
import com.enjoyiot.framework.common.util.object.BeanUtils;
import com.enjoyiot.module.infra.api.file.FileApi;
import com.enjoyiot.module.system.controller.admin.sp.vo.SpCreateReqVO;
import com.enjoyiot.module.system.controller.admin.sp.vo.SpPageReqVO;
import com.enjoyiot.module.system.controller.admin.sp.vo.SpRespVO;
import com.enjoyiot.module.system.controller.admin.sp.vo.SpUpdateReqVO;
import com.enjoyiot.module.system.dal.dataobject.sp.SpDO;
import com.enjoyiot.module.system.service.sp.SpService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.enjoyiot.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 视频地址接口
 */
@RestController
@RequestMapping("/system/sp")
@Validated
public class SpController {

    @Resource
    private SpService spService;
    
    @Resource
    private FileApi fileApi;

    /**
     * 创建视频地址
     */
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('system:sp:create')")
    public CommonResult<Long> createSp(@Valid @RequestBody SpCreateReqVO createReqVO) {
        return success(spService.createSp(createReqVO));
    }

    /**
     * 更新视频地址
     */
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermission('system:sp:update')")
    public CommonResult<Boolean> updateSp(@Valid @RequestBody SpUpdateReqVO updateReqVO) {
        spService.updateSp(updateReqVO);
        return success(true);
    }

    /**
     * 删除视频地址
     *
     * @param id 编号
     */
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPermission('system:sp:delete')")
    public CommonResult<Boolean> deleteSp(@RequestParam("id") Long id) {
        spService.deleteSp(id);
        return success(true);
    }

    /**
     * 获得视频地址
     *
     * @param id 编号
     */
    @GetMapping("/get")
    @PreAuthorize("@ss.hasPermission('system:sp:query')")
    public CommonResult<SpRespVO> getSp(@RequestParam("id") Long id) {
        SpDO sp = spService.getSp(id);
        return success(BeanUtils.toBean(sp, SpRespVO.class));
    }

    /**
     * 获得视频地址列表
     *
     * @param ids 编号列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:sp:query')")
    public CommonResult<List<SpRespVO>> getSpList(@RequestParam("ids") Collection<Long> ids) {
        List<SpDO> list = spService.getSpList(ids);
        return success(BeanUtils.toBean(list, SpRespVO.class));
    }

    /**
     * 获得所有视频地址列表
     */
    @GetMapping("/list-all")
    @PreAuthorize("@ss.hasPermission('system:sp:query')")
    public CommonResult<List<SpRespVO>> getSpAllList() {
        List<SpDO> list = spService.getSpList();
        return success(BeanUtils.toBean(list, SpRespVO.class));
    }

    /**
     * 获得视频地址分页
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:sp:query')")
    public CommonResult<PageResult<SpRespVO>> getSpPage(@Valid SpPageReqVO pageVO) {
        PageResult<SpDO> pageResult = spService.getSpPage(pageVO);
        return success(BeanUtils.toBean(pageResult, SpRespVO.class));
    }

    /**
     * 根据视频编号获取视频地址
     *
     * @param spNumber 视频编号
     */
    @GetMapping("/get-by-sp-number")
    @PreAuthorize("@ss.hasPermission('system:sp:query')")
    public CommonResult<SpRespVO> getSpBySpNumber(@RequestParam("spNumber") String spNumber) {
        SpDO sp = spService.getSpBySpNumber(spNumber);
        return success(BeanUtils.toBean(sp, SpRespVO.class));
    }

    /**
     * 根据传感器编号获取视频地址列表
     *
     * @param cgqNumber 传感器编号
     */
    @GetMapping("/list-by-cgq-number")
    @PreAuthorize("@ss.hasPermission('system:sp:query')")
    public CommonResult<List<SpRespVO>> getSpListByCgqNumber(@RequestParam("cgqNumber") Integer cgqNumber) {
        List<SpDO> list = spService.getSpListByCgqNumber(cgqNumber);
        return success(BeanUtils.toBean(list, SpRespVO.class));
    }
    
    /**
     * 上传视频图片
     *
     * @param file 图片文件
     * @return 图片访问URL
     */
    @PostMapping("/upload-image")
    @PreAuthorize("@ss.hasPermission('system:sp:create')")
    public CommonResult<String> uploadSpImage(@RequestParam("file") MultipartFile file) throws Exception {
        String url = fileApi.createFile(file.getOriginalFilename(), null, file.getBytes());
        return success(url);
    }
}